#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import xml.sax
import sys
import time


class PubmedHandler(xml.sax.ContentHandler):
    def __init__(self):
        self.CurrentData = ""
        self.Owner = ""
        self.Status = ""
        self.PMIDVersion = ""
        self.PubModel = ""
        self.IssnType = ""
        self.JournalTitle = ""
        self.ISOAbbreviation = ""
        self.ArticleTitle = ""
        self.Language = ""
        self.PublicationType = ""
        self.PublicationStatus = ""
        self.ArticleIdType = ""
        self.isArticle = 0
        self.isPubmedData = 0

    def startElement(self, tag, attributes):
        self.CurrentData = tag
        if tag == "MedlineCitation":
            self.Owner = attributes["Owner"]
            self.Status = attributes["Status"]
        elif tag == "PMID":
            self.PMIDVersion = attributes["Version"]
        elif tag == "Article":
            self.isArticle = 1
        elif tag == "PubmedData":
            self.isPubmedData = 1
        elif self.isArticle == 1:
            if tag == "Article":
                self.PubModel = attributes["PubModel"]
            elif tag == "ISSN":
                self.IssnType = attributes["IssnType"]
        elif self.isPubmedData == 1:
            if tag == "ArticleId":
                self.ArticleIdType = attributes["IdType"]


    def endElement(self, tag):
        self.CurrentData = ""
        if tag == "Article":
            self.isArticle = 0
        if tag == "PubmedData":
            self.isPubmedData = 0
        if tag == "PubmedArticle":
            # print(
            #     "Owner=" + self.Owner + "; Status=" + self.Status + "; PMIDVersion=" + self.PMIDVersion + "; PubModel=" + self.PubModel + "; IssnType=" + self.IssnType + "; JournalTitle=" + self.JournalTitle + "; ISOAbbreviation=" + self.ISOAbbreviation + "; ArticleTitle=" + self.ArticleTitle + "; Language=" + self.Language + "; PublicationType=" + self.PublicationType + "; PublicationStatus=" + self.PublicationStatus + "; ArticleIdType=" + self.ArticleIdType + ".")
            self.CurrentData = ""
            self.Owner = ""
            self.Status = ""
            self.PMIDVersion = ""
            self.PubModel = ""
            self.IssnType = ""
            self.JournalTitle = ""
            self.ISOAbbreviation = ""
            self.ArticleTitle = ""
            self.Language = ""
            self.PublicationType = ""
            self.PublicationStatus = ""
            self.ArticleIdType = ""
            self.isArticle = 0
            self.isPubmedData = 0


    def characters(self, content):
        if self.isArticle == 1:
            if self.CurrentData == "Title":
                self.JournalTitle += content
            elif self.CurrentData == "ISOAbbreviation":
                self.ISOAbbreviation += content
            elif self.CurrentData == "ArticleTitle":
                self.ArticleTitle += content
            elif self.CurrentData == "Language":
                self.Language += content
            elif self.CurrentData == "PublicationType":
                self.PublicationType += content
        elif self.isPubmedData == 1:
            if self.CurrentData == "PublicationStatus":
                self.PublicationStatus += content


if __name__ == "__main__":
    time1=time.time()
    # 创建一个 XMLReader
    parser = xml.sax.make_parser()
    # turn off namepsaces
    parser.setFeature(xml.sax.handler.feature_namespaces, 0)

    # 重写 ContextHandler
    Handler = PubmedHandler()
    parser.setContentHandler(Handler)

    parser.parse(sys.argv[1])
    time2=time.time()
    time3=time2-time1
    print time3